package com.itheima.web.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Role;
import com.itheima.domain.Result;
import com.itheima.factory.BeanFactory;
import com.itheima.service.RoleService;
import com.itheima.utils.BeanUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/system/role/*")
public class RoleServlet extends BaseServlet {
private    RoleService roleService = BeanFactory.getService("roleService", RoleService.class);

    private Result findRoleIdsByUserId(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            String userId = req.getParameter("userId");
            //2 调用service层方法查询用户绑定的角色id们
            String[] checkedRoleIds = roleService.findRoleIdsByUserId(userId);
            //3 对客户的作出响应
            return new Result(true,MessageConstant.QUERY_PERMISSIONLIST_SUCCESS,checkedRoleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_PERMISSIONLIST_FAILE);
        }
    }

    private Result updateRoleAndModules(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            String roleId = req.getParameter("roleId");
            String[] moduleIds = BeanUtil.fillBeanFromJson(req, String[].class);
            //2 调用service层方法获取分页结果
            roleService.updateRoleAndModules(roleId,moduleIds);
            //3 对客户的作出响应
            return new Result(true,MessageConstant.SETTING_ROLE_AUTHORIZE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SETTING_ROLE_AUTHORIZE_FAIL);
        }
    }

    private Result findAll(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //2 调用service层方法获取分页结果
            List<Role> list= roleService.findAll();
            //3 对客户的作出响应
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    private Result delete(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数并封装成Role对象
            String[] ids = BeanUtil.fillBeanFromJson(req, String[].class);
            //2 调用service层方法添加数据
            roleService.delete(ids);
            //3 响应结果
            return new Result(true,MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ROLE_FAIL);
        }
    }

    private Result edit(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数并封装成Role对象
            Role role = BeanUtil.fillBeanFromJson(req, Role.class);
            //2 调用service层方法添加数据
            roleService.update(role);
            //3 响应结果
            return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    private Result findById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数
            String id = req.getParameter("id");
            //2 调用service层方法获取分页结果
            Role role = roleService.findById(id);
            //3 对客户的作出响应
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    private Result save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数并封装成Role对象
            Role role = BeanUtil.fillBeanFromJson(req, Role.class);
            //2 调用service层方法添加数据
            roleService.save(role);
            //3 响应结果
            return new Result(true,MessageConstant.SAVE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.SAVE_ROLE_FAIL);
        }
    }

    /*
     * 处理分页查询的请求
     * @param request
     * @param response
     */
    private Result findByPage(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
            //1 获取请求参数（当前页数和每页条数）
            String currentPage1 = req.getParameter("currentPage");
            String pageSize1 = req.getParameter("pageSize");
            //2.判断是否为空
            int currentPage = 1,pageSize = 5;
            if (!StringUtils.isBlank(currentPage1)){
                //3.给页码和条数赋默认值
                currentPage = Integer.parseInt(currentPage1);

            }
            if (!StringUtils.isBlank(pageSize1)){
                //3.给页码和条数赋默认值
                pageSize = Integer.parseInt(pageSize1);

            }
            //4.调用Service层方法获得结果
            PageInfo<Role> page = roleService.findByPage(currentPage, pageSize);
            //5.给客户端响应
            return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
        }
    }

//    @Override
//    protected Result doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, Exception {
//        doGet(req, resp);
//    }


}

